/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.db;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.feiyue.basic.server.parser.Book;
import java.util.ArrayList;
import java.util.List;

public class BookCollectDBHandler {
	public static final String COLUMN_author = "author";
	public static final String COLUMN_categoryid = "categoryid";
	public static final String COLUMN_coveraddr = "coveraddr";
	public static final String COLUMN_coverid = "coverid";
	public static final String COLUMN_dlstate = "dlstate";
	public static final String COLUMN_id = "id";
	public static final String COLUMN_introduction = "introduction";
	public static final String COLUMN_issuestate = "issuestate";
	public static final String COLUMN_name = "name";
	public static final String CREATE_TABLE_booklist = "CREATE TABLE IF NOT EXISTS bookcollectlist (categoryid INTEGER not null, id INTEGER PRIMARY KEY not null, name text not null, author text not null, coveraddr text not null, introduction text not null, issuestate text not null, coverid INTEGER not null, dlstate INTEGER not null);";
	public static final String myTableName = "bookcollectlist";
	protected SQLiteDatabase mSQLiteDatabase = null;

	public BookCollectDBHandler(Context paramContext) throws Exception {
		this.mSQLiteDatabase = paramContext.openOrCreateDatabase("novel", 0,
				null);
		this.mSQLiteDatabase
				.execSQL("CREATE TABLE IF NOT EXISTS bookcollectlist (categoryid INTEGER not null, id INTEGER PRIMARY KEY not null, name text not null, author text not null, coveraddr text not null, introduction text not null, issuestate text not null, coverid INTEGER not null, dlstate INTEGER not null);");
	}

	public void delete(Context paramContext, Book paramBook) {
		String str = "delete from bookcollectlist where id = "
				+ paramBook.getId();
		this.mSQLiteDatabase.execSQL(str);
		Intent localIntent = new Intent("MESSAGERECEIVER_BOOKID");
		localIntent.putExtra("bookid", paramBook.getId());
		paramContext.sendBroadcast(localIntent);
	}

	public void destroy() {
		if (this.mSQLiteDatabase == null)
			return;
		this.mSQLiteDatabase.close();
	}

	public Book getBook(Context paramContext, int paramInt) {
		String str1 = "select * from bookcollectlist where id = " + paramInt;
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str1, null);
			Book localBook = null;
			if (localCursor != null && localCursor.moveToFirst()) {
				localBook = new Book();
				String str2 = localCursor.getString(localCursor
						.getColumnIndex("coveraddr"));
				String str3 = localCursor.getString(localCursor
						.getColumnIndex("name"));
				int i = localCursor.getInt(localCursor.getColumnIndex("id"));
				String str4 = localCursor.getString(localCursor
						.getColumnIndex("author"));
				String str5 = localCursor.getString(localCursor
						.getColumnIndex("introduction"));
				int j = localCursor.getInt(localCursor
						.getColumnIndex("categoryid"));
				int k = localCursor.getInt(localCursor
						.getColumnIndex("coverid"));
				int l = localCursor.getInt(localCursor
						.getColumnIndex("dlstate"));
				String str6 = localCursor.getString(localCursor
						.getColumnIndex("issuestate"));
				localBook.setCoverAddr(str2);
				localBook.setName(str3);
				localBook.setId(i);
				localBook.setAuthor(str4);
				localBook.setIntroduction(str5);
				localBook.setIssuestate(str6);
				localBook.setCategoryId(j);
				localBook.setCoverid(k);
				localBook.setdlstate(l);
			}
			return localBook;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return null;
	}

	public List<Book> getBookList(Context paramContext) {
		ArrayList localArrayList = new ArrayList();
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(
					"select * from bookcollectlist", null);
			if (localCursor != null && localCursor.moveToFirst())
				do {
					Book localBook = new Book();
					String str1 = localCursor.getString(localCursor
							.getColumnIndex("coveraddr"));
					String str2 = localCursor.getString(localCursor
							.getColumnIndex("name"));
					int i = localCursor
							.getInt(localCursor.getColumnIndex("id"));
					String str3 = localCursor.getString(localCursor
							.getColumnIndex("author"));
					String str4 = localCursor.getString(localCursor
							.getColumnIndex("introduction"));
					int j = localCursor.getInt(localCursor
							.getColumnIndex("categoryid"));
					int k = localCursor.getInt(localCursor
							.getColumnIndex("coverid"));
					int l = localCursor.getInt(localCursor
							.getColumnIndex("dlstate"));
					String str5 = localCursor.getString(localCursor
							.getColumnIndex("issuestate"));
					localBook.setCoverAddr(str1);
					localBook.setName(str2);
					localBook.setId(i);
					localBook.setAuthor(str3);
					localBook.setIntroduction(str4);
					localBook.setIssuestate(str5);
					localBook.setCategoryId(j);
					localBook.setCoverid(k);
					localBook.setdlstate(l);
					localArrayList.add(localBook);
				} while (localCursor.moveToNext());
			
			return localArrayList;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		return null;
	}

	public void insert(Book paramBook, Context paramContext) {
		if (isExist(paramBook))
			return;
		String str = "insert into bookcollectlist values("
				+ paramBook.getCategoryId() + "," + paramBook.getId() + ", \""
				+ paramBook.getName().replace("\"", "") + "\", \""
				+ paramBook.getAuthor().replace("\"", "") + "\", \""
				+ paramBook.getCoverAddr() + "\", \""
				+ paramBook.getIntroduction().replace("\"", "") + "\"" + " ,\""
				+ paramBook.getIssuestate() + "\" " + ","
				+ paramBook.getCoverid() + "," + paramBook.getdlstate() + ");";
		this.mSQLiteDatabase.execSQL(str);
		Intent localIntent = new Intent("MESSAGERECEIVER_BOOKID");
		localIntent.putExtra("bookid", paramBook.getId());
		paramContext.sendBroadcast(localIntent);
	}

	public void insert(List<Book> paramList, Context paramContext) {
		if (paramList == null)
			return;
		int i = paramList.size();
		for (int j = 0;; ++j) {
			if (j < i)
				;
			insert((Book) paramList.get(j), paramContext);
		}
	}

	public boolean isExist(Book paramBook) {
		String str = "select * from bookcollectlist where id = "
				+ paramBook.getId();
		Cursor localCursor = null;
		try {
			localCursor = this.mSQLiteDatabase.rawQuery(str, null);
			if (localCursor != null && localCursor.moveToFirst()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (localCursor != null) {
				localCursor.close();
			}
		}
		
		return false;
	}

	public void set(Book paramBook, Context paramContext) {
		if (!(isExist(paramBook)))
			return;
		String str = "update bookcollectlist set dlstate = "
				+ paramBook.getdlstate() + " where id = " + paramBook.getId();
		this.mSQLiteDatabase.execSQL(str);
	}
}